package com.base.cn.platform.os.controller.user.exprule;


import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.exprule.CusUserExpRule;
import com.base.cn.platform.os.entity.user.exprule.CusUserExpRuleColumns;
import com.base.cn.platform.os.service.user.exprule.CusUserExpRuleBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 经验接口
 *
 * @author ll
 * @create 2018-04-10-13:36
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserExpRuleController extends BaseController {

    @Autowired
    private CusUserExpRuleBiz cusUserExpruleBiz;


    /**
     * 添加经验规则
     * @param cusUserExprule 经验数据
     * @return
     */
    @RequestMapping("/saveUpdateCusUserExpRule")
    public Map<String, Object> saveUpdateCusUserExpRule(@RequestBody CusUserExpRule cusUserExprule){
        Map<String,Object> _map = checkUserExpRule(cusUserExprule);
        if (!ObjectUtils.isNotEmpty(_map)){
            cusUserExprule =cusUserExpruleBiz.saveCusUserExpRule(cusUserExprule);
            return ResultUtil.SUCCESS("添加成功",cusUserExprule);
        }
        return _map;
    }

    /**
     * 翻页搜索经验
     * @param cusUserExprule
     * @param currentPage
     * @return
     */
    @RequestMapping("/queryCusUserExpRulePage")
    public PageInfo<CusUserExpRule> queryCusUserExpRulePage(@RequestBody CusUserExpRuleColumns cusUserExprule,
                                                            @RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserExpruleBiz.queryCusUserExpRulePage(cusUserExprule,page);
    }

    /**
     * 不翻页翻页搜索经验
     * @param cusUserExprule
     * @return
     */
    @RequestMapping("/queryCusUserExpRuleList")
    public List<CusUserExpRule> queryCusUserExpRuleList(@RequestBody CusUserExpRuleColumns cusUserExprule,
                                                        @RequestParam(value = "containColumns",required = false)List<String> containColumns){
        return cusUserExpruleBiz.queryCusUserExpRuleList(cusUserExprule,containColumns);
    }

    /**
     * 修改经验状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateCusUserExpRuleStatus")
    public CusUserExpRule updateCusUserExpRuleStatus(@RequestParam("id")BigDecimal id,
                                                     @RequestParam("status")Integer status){
        if (!DataUtil.idIsNotNull(id)||status<=0){
            return null;
        }
        return cusUserExpruleBiz.updateUserExpRuleStatus(id,status);
    }

    /**
     * 根据Id搜索
     * @param id
     * @return
     */
    @RequestMapping("/queryCusUserExpRuleFindById")
    public CusUserExpRule queryCusUserExpRuleFindById(@RequestParam("id")BigDecimal id,
                                                      @RequestParam("containColumns")List<String> containColumns){
        if (!DataUtil.idIsNotNull(id)){
            return null;
        }
        return cusUserExpruleBiz.findById(id,containColumns);
    }

    //=================================================================================================
    /**
     * 添加修改方法
     * @param userExprule
     * @return
     */
    private Map<String,Object> checkUserExpRule(CusUserExpRule userExprule){
        if(!DataUtil.idIsNotNull(userExprule.getId())){
            if(!StringUtils.isNotEmpty(userExprule.getAction())){
                return ResultUtil.ERROR("请填写行为说明");
            }else if(!ObjectUtils.isNotEmpty(userExprule.getActionType())){
                return ResultUtil.ERROR("请填写行为类型");
            }else if(!ObjectUtils.isNotEmpty(userExprule.getType())){
                return ResultUtil.ERROR("请填写获取经验类型");
            }else if(!ObjectUtils.isNotEmpty(userExprule.getScore())){
                return ResultUtil.ERROR("请填写获取经验数");
            }else if(userExprule.getType()==2&&StringUtils.isEmpty(userExprule.getTermDepict())){
                return ResultUtil.ERROR("请填写百分比说明");
            }else if(StringUtils.isEmpty(userExprule.getActionKey())){
                return ResultUtil.ERROR("请填写使用场景");
            }
        }
        if(DataUtil.idIsNotNull(userExprule.getId())) {
            if (!ObjectUtils.isNotEmpty(userExprule.getScore())) {
                return ResultUtil.ERROR("请填写获取经验数");
            } else if (!StringUtils.isNumber(userExprule.getScore().toString())) {
                return ResultUtil.ERROR("经验为不为0的正整数");
            }
        }
        return null;
    }

}
